package com.sdy.hospitalguard.embed.keepalive.manager.netty.server;

import com.sdy.hospitalguard.embed.keepalive.manager.netty.hander.HttpHeartHandler;
import com.sdy.hospitalguard.embed.keepalive.manager.netty.hander.HttpResponseHander;
import com.sdy.hospitalguard.embed.keepalive.manager.netty.netty.codec.HttpDecoder;
import com.sdy.hospitalguard.embed.keepalive.manager.netty.netty.codec.HttpEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.concurrent.TimeUnit;

public class LapiServerInitializer extends ChannelInitializer<SocketChannel> {

    private final int READ_TIME_OUT = 1200;
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
        // 通道空闲超时时间
        pipeline.addLast(new IdleStateHandler(READ_TIME_OUT, READ_TIME_OUT, READ_TIME_OUT, TimeUnit.SECONDS));
        pipeline.addLast(new HttpDecoder());
        pipeline.addLast(new HttpEncoder());
        pipeline.addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));

        // 保活超时时间
        pipeline.addLast(new ReadTimeoutHandler(READ_TIME_OUT));

        // 心跳
        pipeline.addLast(new HttpHeartHandler());
        // 响应入队列
        pipeline.addLast(new HttpResponseHander());

    }

}
